package io.wjc.config;

import io.wjc.common.MyKeyGenerator;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

/**
 * 此配置用來测试自定义KeyGenerator，经过测试发现ehcache.xml必须存在一个cache name和 @Cacheable(value = "books") 注解中的value匹配
 */
//@Configuration
//@ComponentScan(basePackages = "io.wjc")
//@EnableCaching(proxyTargetClass = true)
public class AppConfig extends CachingConfigurerSupport {
    @Bean
    @Override  
    public CacheManager cacheManager() {
  
        try {  
            net.sf.ehcache.CacheManager ehcacheCacheManager  
                    = new net.sf.ehcache.CacheManager(new ClassPathResource("ehcache.xml").getInputStream());
  
            EhCacheCacheManager cacheCacheManager = new EhCacheCacheManager(ehcacheCacheManager);
            return cacheCacheManager;  
        } catch (IOException e) {
            throw new RuntimeException(e);  
        }  
    }

    @Bean
    @Override  
    public KeyGenerator keyGenerator() {
        return new MyKeyGenerator();
    }
}